<?php

namespace app\modules\backend\controllers;

use Yii;
use app\tools\Re;
use app\models\Base;
use app\models\GoodCate;

class GoodCateController extends BaseController
{
  /**
   * @param string $type
   * @return array
   */
  public function actionList(){
    $name = Yii::$app->request->post('name', '');
    $page = Yii::$app->request->post('page', 0);
    $page_size = Yii::$app->request->post('page_size', 20);
    $query = GoodCate::find()->where(['status' => [0,1]]);
    if (!empty($name)) {
      $query->andWhere(['like', 'name', $name]);
    }
    $list = $query->orderBy('sort desc, created_at desc')->limit($page_size)->offset($page_size*$page)->all();
    $data = [];
    if ($list) {
      foreach($list as $v) {
        $data[] = [
          'id' => $v->id,
          'parent_id' => $v->parent_id,
          'parent_name' => $v->parent_id ? $v->parent->name : '',
          'name' => $v->name,
          'sub_name' => $v->sub_name,
          'bg_cover' => $v->bg_cover,
          'icon' => $v->icon,
          'sort' => $v->sort,
          'status' => $v->status,
          'status_name' => $v->status ? '启用' : '禁用',
          'created_time' => date('Y-m-d H:i:s', $v->created_at)
        ];
      }
    }
    return Re::data([
      'data' => $data,
      'totalPage' => $query->count(),
      'currentPage' => $page,
      'perPage' => $page_size
    ]);
  }

  public function actionEdit(){
    $id = Yii::$app->request->post('id', 0);
    $post = Yii::$app->request->post();
    $keys = array_keys($post);
    $first_key = $keys[0];
    if (!$first_key) {
      return;
    }
    return $this->edit(GoodCate::class, $id, $post);
  }

  public function actionChangeStatus(){
    $id = Yii::$app->request->post('id', 0);
    $status = Yii::$app->request->post('status', 0);
    $model = GoodCate::findOne($id);
    if (empty($model)) {
      return Re::data(null, '商品分类信息读取失败', Re::ERROR);
    }
    $model->status = $status;
    if (!$model->save()) {
      return Re::data(null, '操作失败', Re::ERROR);
    }
    return Re::data();
  }

  public function actionParentListForSelect(){
    $data = GoodCate::find()->where(['status' => 1, 'parent_id' => 0])->select("id, name")->orderBy('sort desc, created_at desc')->all();
    return Re::data(['data' => $data,]);
  }

  public function actionAllList(){
    $all_data = [];
    $parent_list = GoodCate::find()->where(['status' => 1, 'parent_id' => 0])->orderBy('sort desc, created_at desc')->all();
    if (!empty($parent_list)) {
      foreach ($parent_list as $v) {
        $child_data = [];
        $child_list = GoodCate::find()->where(['parent_id' => $v->id, 'status' => 1])->orderBy('sort desc, created_at desc')->all();
        if (!empty($child_list)) {
          foreach ($child_list as $vv) {
            $child_data[] = [
              'value' => $vv->id,
              'label' => $vv->name
            ];
          }
        }
        $all_data[] = [
          'value' => $v->id,
          'label' => $v->name,
          'children' => $child_data
        ];
      }
    }
    return Re::data(['data' => $all_data]);
  }
}
